<?php
/*
 * Created on Apr 30, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
require_once("model/DenormRow.php");

class DataMigraDAO{
	
	var $conn;
	
	public function __construct($conn){
		$this->conn =& $conn;
	}
	
	public function importResult(){
		try{
			if($stmt = $this->conn->prepare("CALL IMPORT_RESULTS()")){
				$stmt->execute();
				$stmt->close();	
			}
			else{
				printf("Failed to prepare statement in \"importResult\" method of %s",__CLASS__);
			}	
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
		$this->conn->close();
	}
	
	public function buildSnapshot($eventid){
		try{
			if($stmt = $this->conn->prepare("CALL GET_RESULT_SNAPSHOT(?)")){
				$stmt->bind_param("i",$eventid);
				$stmt->execute();
			}
			else{
				printf("Failed to prepare statement in \"buildSnapshot\" method of %s",__CLASS__);
			}
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
 		
 		$this->conn->close();
	}
	
	public function displayContents($importStatusId){
		$output = null;
		
		try{
			if($stmt = $this->conn->prepare("CALL DENORMALIZED_RESULTS_DISPLAY(?)")){
				$stmt->bind_param("i",$importStatusId);
				$stmt->execute();
				$stmt->bind_result($_rowId,$_fname,$_lname,$_emailAddr,$_crse,$_1,$_2,$_3,$_4,$_5,$_6,$_7,$_8,$_9,$_10,$_11,$_12,$_13,$_14,$_15,$_16,$_17,$_18,$_pid,$_cid,$_eid,$_valid);
				
				$output = array();
				
				while($stmt->fetch()){
					$row = new DenormRow();
					$row->rowid = $_rowId;
					$row->firstname = $_fname;
					$row->lastname = $_lname;
					$row->emailaddress = $_emailAddr;
					$row->course = $_crse;
					$row->h1 = $_1;
					$row->h2 = $_2;
					$row->h3 = $_3;
					$row->h4 = $_4;
					$row->h5 = $_5;
					$row->h6 = $_6;
					$row->h7 = $_7;
					$row->h8 = $_8;
					$row->h9 = $_9;
					$row->h10 = $_10;
					$row->h11 = $_11;
					$row->h12 = $_12;
					$row->h13 = $_13;
					$row->h14 = $_14;
					$row->h15 = $_15;
					$row->h16 = $_16;
					$row->h17 = $_17;
					$row->h18 = $_18;
					$row->playerid = $_pid;
					$row->courseid = $_cid;
					$row->eventid = $_eid;
					$row->valid = $_valid;
					array_push($output,$row);
				}
			}
			else{
				echo "Failed to prepare statement.";
			}	
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
		$stmt->close();
		$this->conn->close();
		return $output;
	}
	
	public function listNewPlayers($importStatusId){
		
		if($stmt = $this->conn->prepare("CALL DENORMALIZED_RESULTS_LIST_NEW_PLAYERS(?)")){
			$stmt->bind_param("i",$importStatusId);
			$stmt->execute();
			
			$stmt->bind_result($_minRowID,$_firstName,$_lastName,$_emailAddress);
			
			$result = array();
			while($stmt->fetch()){
				$p = new Player();
				$p->playerid = $_minRowID;
				$p->firstName = $_firstName;
				$p->lastName = $_lastName;
				$p->emailAddress = $_emailAddress;
				array_push($result,$p);
			}
			
			$stmt->close();	
		}
		$this->conn->close();
		
		return $result;
	}
	
	public function updatePlayer($orig,$new){
		
		try{
			if($stmt = $this->conn->prepare("CALL DENORMALIZED_RESULTS_UPDATE_PLAYER(?,?,?,?,?,?)")){
				$stmt->bind_param("ssssss",$orig->firstName,$orig->lastName,$orig->emailAddress,$new->firstName,$new->lastName,$new->emailAddress);
				$stmt->execute();
				$stmt->close();	
			}
			else{
				return "{\"status\":\"Failed to prepare the statement\"}";
			}	
		}
		catch(Exception $e){
			return sprintf("{\"status\":\"%s\"}",$e->getMessage());
			
		}
		$this->conn->close();
		
		return "{\"status\":\"succeeded.\"}";
	}
	
	public function fillInIds(){
		
		try{
			if($stmt = $this->conn->prepare("CALL DENORMALIZED_RESULTS_FILLIN()")){
				$stmt->execute();
				$stmt->close();
			}
			else{
				throw new Exception("Failed to prepare the statement.");
			}
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		$this->conn->close();
	}
	
	public function insertNewPlayers(){
		
		try{
			if($stmt = $this->conn->prepare("CALL DENORMALIZED_RESULTS_INSERT_NEW_PLAYERS()")){
				$stmt->execute();
				$stmt->close();
			}
			else{
				throw new Exception("Failed to prepare the statement.");
			}
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		$this->conn->close();
	}
	
	public function listNewCourses($importStatusId){
		
		$result = array();
		try{
			if($stmt = $this->conn->prepare("CALL DENORMALIZED_RESULTS_LIST_NEW_COURSES(?)")){
				$stmt->bind_param("i",$importStatusId);
				$stmt->execute();
				$stmt->bind_result($_course);
				while($stmt->fetch()){
					array_push($result,$_course);
				}
				$stmt->close();
			}
			else{
				throw new Exception("Failed to prepare the statement.");
			}
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		$this->conn->close();
		return $result;
	}
	
	public function importResults($importStatusId){
		
		try{
			if($stmt = $this->conn->prepare("CALL IMPORT_RESULTS(?)")){
				$stmt->bind_param("i",$importStatusId);
				$stmt->execute();
				$stmtError = $stmt->error;
				$stmt->close();
				if($stmtError != ""){
					throw new Exception("Failed in \"importResults\": ".$stmtError);
				}				
			}
			else{
				throw new Exception("Failed to prepare the statement.");
			}
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		$this->conn->close();
	}
	
	public function getSnapshot($eventid){
		
		try{
			if($stmt = $this->conn->prepare("CALL GET_RESULT_SNAPSHOT(?)")){
				$stmt->bind_param("i",$eventid);
				$stmt->execute();
				echo $stmt->error;
				$stmt->close();
			}
			else{
				$msg = "Failed to prepare the statement.";
			}
		}
		catch(Exception $e){
			$msg = $e->getMessage();
		}
		$this->conn->error;
		$this->conn->close();
		return $msg;
	}

	public function bulkInsert($qry){
		try{
			if($stmt = $this->conn->prepare($qry)){
				$stmt->execute();
			}
			else{
				throw new Exception($this->conn->error);
			}
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		$stmtError = $stmt->error;
		$this->conn->close();
		if($stmtError != ""){
			throw new Exception("Failed in \"importResults\": ".$stmtError);
		}
	}
}
?>
